<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use app\common\util\TreeUtil;
use \think\Db;

/**
 * 栏目管理
 * @author xiegaolei
 *
 */
class Column extends Base
{
	
    /**
     * 栏目列表
     */
    public function index(){
    
    	if ($this->request->isPost()){
    
    		$draw = input('draw',1,'intval');
    		//排序设置
    		$mycolumns = input('mycolumns/a','');
    		$myorder = input('order/a','');
    		if (empty($myorder)||empty($mycolumns)) {
    			$order = 'id asc';
    		}else{
    			$order = '';
    			foreach ($myorder as $key => $v) {
    				$order .= $mycolumns[$v['column']].' '.$v['dir'].' ,';
    			}
    			$order = rtrim($order , ',');
    		}
    
    		$roles = Db::name('column');
    		$lists = $roles->order($order)->select();
    		foreach ($lists as $key => $v){
    			if ($v['isShow']){
    				$lists[$key]['isShow'] = '正常';
    			}else{
    				$lists[$key]['isShow'] = '禁用';
    			}
    		}
    		//格式化处理
    		$lists = TreeUtil::listToTreeOne( $lists ,  0 , '|— ' , 'id' , 'pid' , 'html');
    		
    		$result['draw'] = $draw;
    		$result['recordsTotal'] = $roles->count();
    		$result['recordsFiltered'] = $result['recordsTotal'];
    		$result['data'] = $lists;
    		 
    		json_return($result);
    		 
    	}else{
    		return $this->fetch();
    	}
    }
    
    
    
    /**
     * 添加
     */
    public function columnAdd(){
    
    	$column = Db::name('column')->where('isShow',1)->order('sorts','asc')->select();
    	//格式化处理
    	$column = TreeUtil::listToTreeOne( $column ,  0 , '|— ' , 'id' , 'pid' , 'html');
    	
    	$this->assign('column', $column);
    	return $this->fetch();
    }
    
    /**
     * 编辑
     */
    public function columnEdit(){
    
    	$id = input('id','0','int');
    	if (empty($id)){
    		exit('参数有误！');
    	}
    	
    	$column = Db::name('column')->where('isShow',1)->order('sorts','asc')->select();
    	//格式化处理
    	$column = TreeUtil::listToTreeOne( $column ,  0 , '|— ' , 'id' , 'pid' , 'html');
    	$this->assign('column', $column);
    	
    	$info = Db::name('column')->where('id',$id)->find();
    	$this->assign('info',$info);
    	
    	return $this->fetch();
    }
    
    /**
     * 保存权限
     */
    public function columnSave(){
    
    	//验证规则
    	$rule = [
    			'title'  => ['require'],
    	];
    
    	//提示信息
    	$msg = [
    			'title' => '名称必填',
    	];
    	//接收数据
    	$data = [
    			'pid'  => input('pid','','int'),
    			'title'  => input('title','','trim'),
    			'descript'  => input('descript','','trim'),
    			'img'  => input('img','','trim'),
    			'isShow' => input('isShow',0,'int')
    	];
    
    	//验证输入数据合法性
    	$validate = new \think\Validate($rule, $msg);
    	$result   = $validate->check($data);
    	if (!$result){
    		$error = $validate->getError();
    		ajax_return_error($error);
    	}                                                                  
    	 
    	$id = input('id','0','int');
    	$column = Db::name('column');
    	if ($id){
    		
    		if($this->checkColumn($data['title']) && $id != $this->checkColumn($data['title'])){
    			ajax_return_error('该名称已存在');
    		}
    		
    		$res = $column->where('id',$id)->update($data);
    	}else{
    		
    		if($this->checkColumn($data['title'])){
    			ajax_return_error('该名称已存在');
    		}
    		
    		$res = $column->insert($data);
    	}
    	 
    	if ($res){
    		  
    		ajax_return_ok([],'操作成功！');
    	}else{
    		ajax_return_error('操作失败！');
    	}
    	 
    }
    
    
    /**
     * [sortColum 栏目排序]
     * @return [type] [description]
     */
    public function columnSort(){
    	$listOrder = $_POST['listOrder'];
    	
    	if (empty($listOrder)) {
    		ajax_return_error('没有数据！');
    	}else{
    		$column = Db::name('column');
    		foreach ($listOrder as $id => $sorts){
    			$column->where('id', $id)->update(['sorts'=>$sorts]);
    		}
    		
    		ajax_return_ok([],'操作成功！');
    	}
    
    }
    
    
    
    /**
     * 删除
     */
    public function columnDel(){
    	$id = input('id','0','int');
    	if ($id == 0) {
    		exit('参数有误！');
    	}else{
    		
    		if (Db::name('article')->where('cid',$id)->find()){
    			ajax_return_error('请先删除该栏目下的文章！');
    		}
    		
    		if (Db::name('column')->where('pid',$id)->find()){
    			ajax_return_error('请先删除子栏目！');
    		}
    		
    		if (Db::name('column')->where('id',$id)->delete()) {
    			ajax_return_ok([],'操作成功！');
    		}else{
    			ajax_return_error('操作失败！');
    		}
    		 
    	}
    }
    
    
    /**
     * [checkName 检测是否存在]
     * @param  [type] $name [description]
     * @return [type]       [description]
     */
    protected function checkColumn($name){
    	$id = Db::name('column')->where('title',$name)->value('id');
    	if (empty($id)) {
    		return 0;
    	}else{
    		return $id;
    	}
    }
    
    
    
}
